<template>
  <div>
    <template v-if="!showDetails">
      <div class="rules-nav">
        <ul style="margin: 0;">
          <li v-for="item in rulesList" :key="item.key" @click="showRules($event, item)">
            <a href="javascript:;" :class="['rules-title', 'flex-row-sb-center', (item.show&&item.next)?'is-selected':'']" @click="item.next? '':getRulesDetail(item)">
              {{item.title}}
              <span  v-if="item.next" :class="['iconfont', 'icon-xiajiantou_huaban',item.show?'icon-animate':'']"></span>
            </a>
            <ul class="sub-ul" v-if="item.next&&item.show">
              <li v-for="i in item.next" :key="i.key" @click="showRules($event, item, i)">
                <a href="javascript:;" @click="i.next? '' :getRulesDetail(i)" :class="['rules-title', 'flex-row-sb-center', (i.show&&i.next)?'is-selected':'']">
                  {{i.title}}
                  <span  v-if="i.next && (i.next.length > 0)" :class="['iconfont', 'icon-xiajiantou_huaban',i.show?'icon-animate':'']"></span>
                </a>
                <ul v-if="i.show" class="sub-ul">
                  <li v-for="items in i.next" @click="items.next? '':getRulesDetail(items)">
                    <a href="javascript:;" class="rules-sub-title">
                      {{items.title}}
                    </a>
                  </li>
                </ul>
              </li>
            </ul>
          </li>
        </ul>
      </div>
      <div class="rules-content" v-if="details.content !== ''">
        <mavon-editor :value="details.content" :subfield="false" defaultOpen="preview" :toolbarsFlag="false" :editable="false" :scrollStyle="true" style="height: auto;">
        </mavon-editor>
      </div>
    </template>
    <template v-else>
      <div class="download-rules-pdf">
        <header class="detail-header flex-row-sb-center">
          <span class="iconfont icon-caidan" @click="showDetails = false , showLink = false"></span>
          <a href="javascript:;" class="download" @click="showLink = !showLink">下载</a>
        </header>
        <p class="load-link" v-if="showLink&&details.link">
          {{details.link}}
          <br>
          复制链接到浏览器打开即可下载！
        </p>
        <div class="rules-content">
          <mavon-editor :value="details.content" :subfield="false" defaultOpen="preview" :toolbarsFlag="false" :editable="false" :scrollStyle="true" style="height: auto;">
          </mavon-editor>
        </div>
      </div>
    </template>
  </div>
</template>

<script>
import { eleRules, getRulesDetails } from '@/api/elm-data'
export default {
  name: 'rules',
  data() {
    return {
      rulesList: [],
      details: {
        content: '',
        link: ''
      },
      showDetails: false,
      pdfName: '',
      showLink: false
    }
  },
  async created() {
    const { data } = await eleRules()
    this.$nextTick(() => {
      const script = document.createElement('script')
      script.innerText = data
      document.body.appendChild(script)
      const interval = setInterval(() => {
        if(window.crayfish) {
          this.rulesList = window.crayfish.config.next
          console.log(this.rulesList)
          clearInterval(interval)
        }
      })
    })
  },
  methods: {
    async showRules(e, rules, subRules) {
      if(subRules||rules.next) {
        e.stopPropagation()
      }
      this.rulesList.forEach(item => {
        if(item.title === rules.title) {
          if(subRules) {
            item.next.forEach(r => {
              if(r.title === subRules.title) {
                r.show = !subRules.show
              }
            })
          }else {
            item.show = !rules.show
          }
        }else {
          item.show = false
        }
      })
    },
    async getRulesDetail(rules) {
      if(rules.key) {
        this.pdfName = rules.title
        console.log(rules)
        const { data } = await getRulesDetails(rules.key)
        console.log(data)
        this.details = data
        this.showDetails = true
      }
    }
  },
  mounted() {
    if(!document.querySelector('.download-rules-pdf')) {
      this.showLink = false
    }
  }
}
</script>

<style scoped lang="less">
  .rules-nav {
    padding-right: 5px;
    background-color: #fff;
    font-size: 30px;
  }
  .icon-xiajiantou_huaban {
    font-size: 18px;
    transition: all .2s ease-in-out;
  }
  .sub-ul {
    padding: 0 20px;
  }
  .rules-title {
    padding: 30px 10px;
    border-bottom: 1px  solid #CCC;
  }
  .rules-sub {
    margin: 25px 0;
  }
  .rules-sub-title {
    display: block;
    padding: 25px 0;
    border-bottom: 1px  solid #CCC;
  }
  .is-selected {
    color: #0097FF;
  }
  .rules-content {
    background-color: #fff;
    padding: 50px 20px;
    border-top: 1px solid #ccc;
  }
  .icon-animate {
    transform: rotate(180deg);
  }
  .download-rules-pdf {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    background: #fff;
    box-sizing: border-box;
  }
  .icon-caidan {
    font-size: 46px;
    color: #00A9FF;
    font-weight: 600;
  }
  .download {
    padding: 10px 20px;
    background-color: #0097FF;
    border-radius: 5px;
    font-size: 28px;
    color: #fff;
  }
  .detail-header {
    box-shadow: 0 5px 5px #dcdcdc;
    padding: 20px;
  }
  .load-link {
    padding: 0 20px;
    text-align: center;
  }
</style>
